﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Controls;

namespace MediaPlayer.ViewModel.Tools
{
    public sealed class PositionToValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TimeSpan fvalue;
            try
            {
                MediaElement me = parameter as MediaElement;

                fvalue = new TimeSpan(me.Position.Days, me.Position.Hours, me.Position.Minutes, me.Position.Seconds, me.Position.Milliseconds);
            }
            catch (Exception)
            {
                TimeSpan ts = (TimeSpan)value;
                fvalue = new TimeSpan(ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
            }
            return fvalue.TotalMilliseconds;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TimeSpan fvalue;
            try
            {
                fvalue = ((MediaElement)parameter).Position;
            }
            catch
            {
                fvalue = TimeSpan.FromMilliseconds((double)value);
            }
            return fvalue;
        }
    }
}